import java.util.*;

public class Test {
    public static void main(String[] args) {
        int k = 2;
        String[] str = {"i","love","kun","love","i"};
        Map<String,Integer> map = new HashMap<>();
        for (String s : str) {
            if (!map.containsKey(s)) {
                map.put(s, 1);
            } else {
                int val = map.get(s);
                map.put(s, val + 1);
            }
        }
        PriorityQueue<Map.Entry<String,Integer>> entries = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if (o1.getValue().compareTo(o2.getValue()) == 0){
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        int ii = k;
        for (Map.Entry<String,Integer> tmp:map.entrySet()) {
            if(ii > 0){
                entries.offer(tmp);
                ii--;
            }else {
               Map.Entry<String,Integer> top = entries.peek();
               Integer val = top.getValue();
               if(val.compareTo(tmp.getValue()) < 0){
                   entries.poll();
                   entries.offer(tmp);
               }else if(val.compareTo(tmp.getValue()) == 0){
                   String topKey = top.getKey();
                   if (topKey.compareTo(tmp.getKey()) > 0){
                       entries.poll();
                       entries.offer(tmp);
                   }
               }
            }
        }
        List<String> list = new ArrayList<>();
        while (!entries.isEmpty()){
            String tmp = entries.poll().getKey();
            list.add(tmp);
        }
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return 0;
            }
        });
        Collections.reverse(list);
        System.out.println(list);
    }
    public static void main2(String[] args) {
        MapSet mapSet = new MapSet();
        MapSet.Node head1 = mapSet.create();
        MapSet.Node head2 = mapSet.deepCopy();
        System.out.println(head1);
        System.out.println(head2);
    }
    public static void main1(String[] args) {
        Map<String,Integer> map = new TreeMap<>();
        map.put("a",3);
        System.out.println(map);
        map.put("b",2);
        System.out.println(map);
    }
}
